package com.news.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.news.db.OpDB;
import com.news.model.News;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 模糊搜索新闻
 */
@WebServlet("/searchServlet")
public class SearchNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String search = req.getParameter("search");
        OpDB opdb = new OpDB();
        ArrayList<News> news = opdb.OpSearchNews(search);
        req.setAttribute("news", news);
        req.getRequestDispatcher("/view/IndexTemp.jsp?mainPage=searchn").forward(req, resp);

        /*ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(news);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);*/
    }
}
